package com.battle.service.repository.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.battle.pojo.entity.User;
import com.battle.pojo.exception.user.UserPhoneRepeatException;
import com.battle.pojo.vo.user.GetUserListVo;
import com.battle.service.mapper.UserMapper;
import com.battle.service.repository.in.UserRepository;
import com.battle.service.utils.StringUtil;
import nirvana.core.exceptions.BizException;
import nirvana.core.repository.Repository;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import com.battle.pojo.dto.user.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * 【描 述】：
 * 【环 境】：J2SE
 * 【详 细】：
 */
@Service
public class UserRepositoryImpl extends Repository<UserMapper, User> implements UserRepository {

    private static final String RANDOM_CODE = "randomCode_";

    private final UserMapper mapper;

    private final StringRedisTemplate stringRedisTemplate;

    public UserRepositoryImpl(UserMapper mapper, StringRedisTemplate stringRedisTemplate) {
        this.mapper = mapper;
        this.stringRedisTemplate = stringRedisTemplate;
    }

    /**
     * 在添加用户签进行校验
     * <p>
     * 校验账号 邮箱 手机号是否在同一部门下有重复
     *
     * @param user
     * @return
     */
    @Override
    public User createToBefore(User user) {
        String account = user.getAccounts();
        String phone = user.getPhone();

        User po = null;
        User byPo = null;

        //校验账号
        po = new User();
        po.setAccounts(account);
        byPo = getByEntity(po);

        if (byPo != null) {
            throw new BizException("账号不能重复");
        }

        //校验手机号
        po = new User();
        po.setPhone(phone);
        byPo = getByEntity(po);

        if (byPo != null) {
            throw new UserPhoneRepeatException();
        }

        return user;
    }


    /**
     * 搜索用户
     *
     * @param dto
     * @return
     */
    @Override
    public Page<GetUserListVo> selectUser(SelectUserDto dto) {
        return mapper.selectUser(dto);
    }

    @Override
    public void saveAuthCode(String authCode, String salt) {
        String key = StringUtil.jointString(RANDOM_CODE, salt, authCode.toLowerCase());
        stringRedisTemplate.opsForValue().set(key, authCode, 180, TimeUnit.SECONDS);
    }

    @Override
    public Boolean checkAuthCode(String authCode, String salt) {
        String key = StringUtil.jointString(RANDOM_CODE, salt, authCode.toLowerCase());
        String code = stringRedisTemplate.opsForValue().get(key);
        boolean hasText = StringUtils.hasText(code);
        if (hasText) {
            //用完一次后删除
            stringRedisTemplate.delete(key);
        }
        return hasText;
    }

    @Override
    public User getUserByAccount(String account) {
        User t = new User();
        t.setAccounts(account);
        return getByEntity(t);
    }

    @Override
    public Map<Long, User> getMap(List<Long> ids) {
        Map<Long, User> map = new HashMap<>();

        if (CollectionUtil.isNotEmpty(ids)) {
            List<User> userList =listByIds(ids);
            if (CollectionUtil.isNotEmpty(userList)) {
                map = userList.stream().collect(Collectors.toMap(User::getId, pojo -> pojo));
            }
        }
        return map;
    }

}